<?php
 class __module_admin_content {
 	
 	function main() {
 		return __driver_display::si()->fetch(array('content/main.html', new KT_start_moduleName('admin')));
 	}
 	
 	function add() {

 		if($_POST['addContent'] == 'true') {
 			
 			$res = __driver_db::si()->query('SELECT id FROM `content` ORDER BY `id` DESC');

        	$row = &$res->fetchRow(DB_FETCHMODE_ASSOC);
        	
        	if(isset($row['id'])) $id = $row['id']+1;
        	else $id = 1;
        	
	 		$res = __driver_db::si()->autoExecute("content", array(
	 						"id"        => $id,
					        "parent_id" => $_POST['add_pid'],
					        "uri_title" => $_POST['add_uri_title'],
	 						"cathegory" => ($_POST['add_thisCat'] == "on"?"1":"0"),
	 						"uid"       => $_SESSION['user']['id']
					), DB_AUTOQUERY_INSERT);
					
			if (PEAR::isError($res)) {
	            exit(print_r($res,true));
	        } else {
	            __driver_db::si()->commit();
	        }
	        
	        foreach($_POST['add'] as $lang=>$array) {
	        	
	        	if(!empty($array['title'])) {
	        	  	
	        		if($array['text'] == "<br>") $array['text'] = "";
	        		
	        	  	$res = __driver_db::si()->autoExecute("content_lang", array(
			 						"title"     => $array['title'],
							        "title_big" => $array['title_big'],
							        "text"      => $array['text'],
			 						"lang"      => $lang,
	        	  					"cid"       => $id
							), DB_AUTOQUERY_INSERT);
							
					if (PEAR::isError($res)) {
			            exit(print_r($res,true));
			        } else {
			            __driver_db::si()->commit();
			        }
	        	  	
	        	  } else $echo = "$lang ignored<br />\n";
	        	
	        }
 			
 			exit($echo);
 		}

 		return __driver_display::si()->fetch(array('content/add.html', new KT_start_moduleName('admin')));
 	}
 	
 	function add_ajax($action) {
 		if($action == "showTree") {
 			
 			if(!isset($_GET['pid']) || empty($_GET['pid'])) $_GET['pid'] = 0;
 			
 			$content = ginst('content_api')->get("", "", $_GET['pid'], false, 1);
 			
 			drv(DISP)->assign('contTree', $content);
 			
 			exit(drv(DISP)->fetch(array('content/ajax/add_showTree.html', new KT_start_moduleName('admin'))));
 		}
 		
 		exit('404');
 	}
 	
 	
 }